/* ----------------------------------------------------------------------------
* File: utils.h
*
* Desc: Header for common utils
*
* Created: 11/03/2011, by Alexey Korshunov
* ---------------------------------------------------------------------------- */
#ifndef _UTILS_H_
#define _UTILS_H_

/************************************************************************/
/*	Returned codes														*/
/************************************************************************/
#define UTILS_OK					0
#define UTILS_ERROR					-1
#define UTILS_TIMEOUT				-2
#define UTILS_CONNECTION_CLOSED		-3



/************************************************************************/
/*	Desc: Create TCP socket                                                   
	Params:
		port - communication port
		log_print - pointer to a log function							
	Return:
		socket descriptor, UTILS_ERROR if error							*/
/************************************************************************/
int UtilsTCPSocket(unsigned short port, int *status, FILE *logFile);


/************************************************************************/
/*	Desc: Create UDP socket                                                   
	Params:
		port - communication port
		log_print - pointer to a log function							
	Return:
		socket descriptor, UTILS_ERROR if error							*/
/************************************************************************/
int UtilsUDPSocket(unsigned short port, int *status, FILE *logFile);


/************************************************************************/
/*	Desc: Listen socket                                                   
	Params:
		sock - socket descriptor
		max_conn - maximum accepted connections
		log_print - pointer to a log function							
	Return:
		UTILS_OK if successful, UTILS_ERROR if error					*/
/************************************************************************/
int UtilsSocketListen(int sock, unsigned int max_conn, int *status, FILE *logFile);


/************************************************************************/
/*	Desc: Accept connection by socket                                                   
	Params:
		sock - socket descriptor
		timeout - time for select waiting
		log_print - pointer to a log function							
	Return:
		accepted socket descriptor, UTILS_ERROR if error				*/
/************************************************************************/
int UtilsSocketAccept(int sock, int timeout, struct sockaddr_in *remote_address, int *status, FILE *logFile);


/************************************************************************/
/*	Desc: Connect to remote host by socket                                                   
	Params:
		sock - socket descriptor
		addr - string containing remote address
		port - remote port for connection
		timeout - time for select waiting
		log_print - pointer to a log function							
	Return:
		one of UTILS_xxx returned codes									*/
/************************************************************************/
int UtilsSocketConnect(int sock, const char* addr, int port, int timeout, int *status, FILE *logFile);


/************************************************************************/
/*	Desc: Send data using TCP transport												
	Params:
		data - pointer to data
		size - data size
		timeout - time for waiting of sending accessibility
		log_print - pointer to a output function
	Return:
		size of sent data if successful,
		UTILS_ERROR if error											*/
/************************************************************************/
int UtilsTCPSend(int sock, const void *data, size_t size, int timeout, int *status, FILE *logFile);


/************************************************************************/
/*	Desc: Receive data using TCP transport												
	Params:
		data - pointer to buffer for receiving
		size - data size
		timeout - time for waiting of receiving accessibility
		log_print - pointer to a output function
	Return:
		size of received data if successful,
		-1 if error														*/
/************************************************************************/
int UtilsTCPReceive(int sock, char *data, size_t size, int timeout, int *status, FILE *logFile);


int UtilsThreadCreate(pthread_t *thread, size_t stack_size, void*(*thread_func)(void*), void *arg, FILE *logFile);
int UtilsThreadCancel(pthread_t *thread, FILE *logFile);
int UtilsThreadKill(pthread_t *thread, int sig, int *status);
int UtilsThreadIsExist(pthread_t *thread);


#endif // _UTILS_H_